package com.alatus.service.impl;

import com.alatus.constant.Constants;
import com.alatus.manager.CustomerManager;
import com.alatus.mapper.TCustomerMapper;
import com.alatus.model.TCustomer;
import com.alatus.query.CustomerQuery;
import com.alatus.result.CustomerExcel;
import com.alatus.service.CustomerService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

@Service
public class CustomerServiceImpl implements CustomerService {
    @Resource
    private TCustomerMapper tCustomerMapper;
    @Resource
    private CustomerManager customerManager;

    @Override
    public Boolean convertCustomer(CustomerQuery customerQuery) {
        return customerManager.transferCustomer(customerQuery);
    }

    @Override
    public PageInfo<TCustomer> getCustomerByPage(Integer current) {
//        设置PageHelper和分页情况
        PageHelper.startPage(current, Constants.PAGE_SIZE);
//        查询
        ArrayList<TCustomer> list = tCustomerMapper.selectCustomerByPage();
//        封装分页到PageInfo中
        PageInfo<TCustomer> info = new PageInfo<>(list);
        return info;
    }

    @Override
    public List<CustomerExcel> getCustomerByExcel(List<String> idList) {
        List<TCustomer> customers = tCustomerMapper.selectCustomerByExcel(idList);
//        把数据转为CustomerExcel
        List<CustomerExcel> customerExcelList = new ArrayList<>();
        customers.forEach(tCustomer -> {
            CustomerExcel customerExcel = new CustomerExcel();
            //需要一个一个设置，没有办法，因为没法使用BeanUtils复制
            customerExcel.setOwnerName(ObjectUtils.isEmpty(tCustomer.getOwnerDO()) ? Constants.EMPTY : tCustomer.getOwnerDO().getName());
            customerExcel.setActivityName(ObjectUtils.isEmpty(tCustomer.getActivityDO()) ? Constants.EMPTY : tCustomer.getActivityDO().getName());
            customerExcel.setFullName(tCustomer.getClueDO().getFullName());
            customerExcel.setAppellationName(ObjectUtils.isEmpty(tCustomer.getAppellationDO()) ? Constants.EMPTY : tCustomer.getAppellationDO().getTypeValue());
            customerExcel.setPhone(ObjectUtils.isEmpty(tCustomer.getClueDO().getPhone()) ? Constants.EMPTY :tCustomer.getClueDO().getPhone());
            customerExcel.setWeixin(ObjectUtils.isEmpty(tCustomer.getClueDO().getWeixin()) ? Constants.EMPTY :tCustomer.getClueDO().getWeixin());
            customerExcel.setQq(ObjectUtils.isEmpty(tCustomer.getClueDO().getQq()) ? Constants.EMPTY :tCustomer.getClueDO().getQq());
            customerExcel.setEmail(ObjectUtils.isEmpty(tCustomer.getClueDO().getEmail()) ? Constants.EMPTY :tCustomer.getClueDO().getEmail());
            customerExcel.setAge(ObjectUtils.isEmpty(tCustomer.getClueDO().getAge()) ? 0 :tCustomer.getClueDO().getAge());
            customerExcel.setJob(ObjectUtils.isEmpty(tCustomer.getClueDO().getJob()) ? Constants.EMPTY :tCustomer.getClueDO().getJob());
            customerExcel.setYearIncome(ObjectUtils.isEmpty(tCustomer.getClueDO().getYearIncome()) ? new BigDecimal(0) : tCustomer.getClueDO().getYearIncome());
            customerExcel.setAddress(ObjectUtils.isEmpty(tCustomer.getClueDO().getAddress()) ? Constants.EMPTY : tCustomer.getClueDO().getAddress());
            customerExcel.setNeedLoadName(ObjectUtils.isEmpty(tCustomer.getNeedLoanDO().getTypeValue()) ? Constants.EMPTY : tCustomer.getNeedLoanDO().getTypeValue());
            customerExcel.setProductName(ObjectUtils.isEmpty(tCustomer.getIntentionProductDO().getName()) ? Constants.EMPTY : tCustomer.getIntentionProductDO().getName());
            customerExcel.setSourceName(ObjectUtils.isEmpty(tCustomer.getSourceDO().getTypeValue()) ? Constants.EMPTY : tCustomer.getSourceDO().getTypeValue());
            customerExcel.setDescription(tCustomer.getDescription());
            customerExcel.setNextContactTime(tCustomer.getNextContactTime());
            customerExcelList.add(customerExcel);
        });
        return customerExcelList;
    }
}

